from general_utilities import *
from graphics_utilities import *
from map import *
from noises import *
from screen_info import display
from sound import sound

class Notification:
  def __init__(self,originating_pos):
    self.originating_pos = originating_pos
    self.noise_info = None # can be (type,volume,generating_entity,audible_to_you)
    self.suppress_noise_msg_if_seen = 0 # set 1 to suppress the printing of the noise message if the event is seen
    self.sound_fileprefix = None # if a Sound is to be played, provide the first part of the .wav filename here
    self.sound_volume_mult = 1
    self.message = None # can put a string here
    self.more = 0 # set 1 to require a keypress after the message
    self.message_color = yellow
    self.message_not_if_noise_heard = 0 # set 1 to suppress the message if the noise above is heard
    self.flash_info = None # can be (pos, tile, seconds)
    self.multiflash_info = None # can be ([pos], tile, seconds)

  def set_noise(self,prob,type,volume,generating_entity,audible_to_you,suppress_if_seen):
    if random.random() < prob:
      self.noise_info = (type,volume,self.originating_pos,generating_entity,audible_to_you)
      self.suppress_noise_msg_if_seen = suppress_if_seen
    
  def set_sound(self,prefix,volume_mult=1):
    # prefix is the first part of the .wav filename, or None
    # supply volume_mult in [0,1]
    self.sound_fileprefix = prefix
    self.sound_volume_mult = volume_mult
    
  def set_message(self,message,not_if_noise_heard,more=0,color=yellow):
    # color is not currently honored!
    # set not_if_noise_heard=1 to suppress the message if the noise above is heard
    self.message = sentencecase(message)
    self.more = more
    self.message_color = color
    self.message_not_if_noise_heard = not_if_noise_heard
    
  def set_flash(self,pos,tile,seconds):
    # 'tile' is the tile object to be flashed up, e.g. display.thump
    self.flash_info = (pos,tile,seconds)
 
  def set_multiflash(self,posses,tile,seconds):
    # 'tile' is the tile object to be flashed up, e.g. display.thump
    self.multiflash_info = (posses,tile,seconds)
    
  def play(self,d):
    seen = 0
    if self.originating_pos is not None:
      if d.get_active_pos().same_room(self.originating_pos):
        seen = 1
    if seen:
      if self.sound_fileprefix is not None:
        sound.play(self.sound_fileprefix,self.sound_volume_mult)
        wait(ms=200)
    if self.flash_info is not None:
      display.draw_flash(d,self.flash_info[0],self.flash_info[1],self.flash_info[2])
    if self.multiflash_info is not None:
      display.draw_multiflash(d,self.multiflash_info[0],self.multiflash_info[1],self.multiflash_info[2])
    noise_heard = 0
    if self.noise_info is not None:
      audible_to_you = self.noise_info[4]
      do_noise_message = audible_to_you and not (self.suppress_noise_msg_if_seen and seen)
      noise_heard = Noise().make(d,self.noise_info[0],self.noise_info[1],self.noise_info[2],self.noise_info[3],do_noise_message)
    if self.message is not None and (seen or self.originating_pos is None):
      if not (noise_heard and self.message_not_if_noise_heard):
        display.send_message(self.message,self.more)

        
      
    
    